package com.ml.mall.api.analytics;

import com.ml.mall.api.BaseController;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.BaseDateRangeReq;
import com.ml.mall.bean.req.admin.category.AdminCategoryQueryPageReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryReportPageReq;
import com.ml.mall.bean.req.admin.supplier.SupplierQueryOneReq;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.service.AnalyticsPartnersService;
import com.ml.mall.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Created by kyyang on 2021/6/8 21:39
 */
@Api(tags = "【analytics】合作商")
@RestController
@RequestMapping("/partners")
@Slf4j
public class AnalyticsPartnersController extends BaseController {

    @Autowired
    private AnalyticsPartnersService analyticsPartnersService;

    //缺少返回：时间范围内所有供应商的每日销售明细
    @RequestMapping(method = RequestMethod.POST, path = "/register_company")
    @ApiOperation("查询合作商名下每日注册公司")
//    @RequiresPermissions(PermissionConst.PARTNER_REGISTER_COMPANY)
    public ApiRestResult partnersRegisterCompany(@Valid @RequestBody BaseDateRangeReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPartnersService.getPartnersRegisterCompany(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/supplier/query_report")
    @ApiOperation("查询供货商卖家每日销售")
//    @RequiresPermissions(PermissionConst.PARTNER_SUPPLIER_QUERY_REPORT)
    public ApiRestResult partnerSupplierQueryReport(@Valid @RequestBody BaseDateRangeReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPartnersService.partnerSupplierQueryReport(req));
    }


    @RequestMapping(method = RequestMethod.POST, path = "/supplier/query_page")
    @ApiOperation("查询合作商下所有卖家 销售额")
//    @RequiresPermissions(PermissionConst.PARTNER_SUPPLIER_QUERY_PAGE)
    public ApiRestResult partnerSupplierQueryPage(@Valid @RequestBody PurchaseQueryReportPageReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPartnersService.partnerSupplierQueryPage(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/supplier/query_one_report")
    @ApiOperation("查询合作商下属某个卖家的统计报告")
//    @RequiresPermissions(PermissionConst.PARTNER_SUPPLIER_QUERY_ONE_REPORT)
    public ApiRestResult partnerSupplierQueryOneReport(@Valid @RequestBody SupplierQueryOneReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPartnersService.partnerSupplierQueryOneReport(req));
    }


    @RequestMapping(method = RequestMethod.POST, path = "/supplier/query_category_sales_page")
    @ApiOperation("查询合作商下属供货商 目录销售额分页")
//    @RequiresPermissions(PermissionConst.PARTNER_SUPPLIER_QUERY_CATEGORY_SALES_PAGE)
    public ApiRestResult partnerSupplierQueryCategorySalesPage(@Valid @RequestBody AdminCategoryQueryPageReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsPartnersService.partnerSupplierQueryCategorySalesPage(req));
    }
}
